Events

Another important C# feature is the event. An event is, essentially, an automatic notification that some action has occurred. they are used to represent things such as keystrokes, mouse clicks, repaint requests, and incoming data. Events are built upon the foundation of the delegate.
An object that has an interest in an event registers an event handler for that event. When the event occurs, all registered handlers are called. Event handlers are represented by delegates.
Events are members of a class and are declared using the event keyword.
Syntax
event event-delegate object-name;

 

The following are some important terms related to events:

Raising an event: The term for invoking or firing an event. When an event is raised, all the methods registered with it are invoked—in order.

Publisher: A class or struct that makes an event available to other classes or structs for their use.

Subscriber: A class or struct that registers methods with a publisher.

Event handler: A method that is registered with an event. It can be declared in the same class or struct as the event, or in a different class or struct.

There are five portions of code that need to be in place to use events:

Delegate type declaration: The event and the event handlers must have a common signature and return type, which is described by the delegate type declaration.

Event handler declarations: The declarations in the subscriber classes, of the methods (the event handlers) to be executed when the event is raised.

Event declaration: The declaration in the publisher class, of the event that holds and
invokes the event handlers.

Event registration: The code that connects the event handlers to the event.

Code that raises the event: The code in the publisher that calls the event, causing it to invoke its event handlers.

Program on Events

using System;
delegate void visionevent();
class abc
{
public event visionevent evt1;

    public void putEvent()
{
if (evt1 != null)
evt1();
}
}

class Vision
{

static void put()
{
Console.WriteLine("Vision Comptuers Sample Event occurred");
}

    public static void Main()
{
abc  x = new abc ();
x.evt1 += put;
x.putEvent();
}
}

Program on Multiple Events

using System;
delegate void visionevent();
class myevent
{
public event visionevent evt1;

.
public void putEvent()
{
if (evt1 != null)
evt1();
}
}

class abc
{
public void put()
{
Console.WriteLine ("Welcome to abc put event");
}
}

class xyz
{
public void put1()
{
Console.WriteLine("Welcome to XYz class put event");
}
}

class Vision
{

static void put()
{
Console.WriteLine("Vision Comptuers Sample Event occurred");
}

    public static void Main()
{
abc   x = new abc ();
xyz y = new xyz();
myevent m = new myevent();
m.evt1 += x.put;
m.evt1 += y.put1;
m.evt1 += put;
m.putEvent();
m.evt1 -= y.put1;
m.putEvent();

    }
}

Program on Anonymous Methods with events
using System;
delegate void visionevent(int x);

 

class myevent
{
public event visionevent evt1;

 

    public void putEvent(int x)
{
if (evt1 != null)
evt1(x);
}
}
class abc
{
public void put(int x)
{
Console.WriteLine ("Welcome to abc put event"+x);
}
}

 

class Vision
{

static void put(int x)
{
Console.WriteLine("Vision Comptuers Sample Event occurred"+x);
}

    public static void Main()
{
abc   x = new abc ();

myevent m = new myevent();
m.evt1 += x.put;
m.evt1 += put;
m.putEvent(5);
m.evt1 += delegate(int p)
{
Console.WriteLine("This is Anynous event delegate"+p);
};
m.putEvent(7);

    }
}

Event Accessors
The second form of the event statement, which allows the use of event accessors. The accessors give you control over how the event handler list is implemented.

event event-delegate event-name {
   add {
      // code to add an event to the chain
   }
 
   remove {
      // code to remove an event from the chain
   }
}

The two event accessors add and remove. The add accessor is called when an event handler is added to the event chain, by using +=. The remove accessor is called when an event handler is removed from the chain, by using =.When add or remove is called, it receives the handler to add or remove as a parameter. As with other types of accessors, this parameter is called value.

Program on Event accessors

using System;
delegate void VisionEvent();

class MyEvent
{
VisionEvent [] evnt = new VisionEvent [3];// Maximum events are 3

    public event VisionEvent  evt
{

add
{
int i;
for (i = 0; i < 3; i++)
if (evnt[i] == null)
{
evnt[i] = value;
break;
}
if (i == 3) Console.WriteLine("Event list full.");
}

       
remove
{
int i;

            for (i = 0; i < 3; i++)
if (evnt[i] == value)
{
evnt[i] = null;
break;
}
if (i == 3) Console.WriteLine("Event handler not found.");
}
}

    // This is called to fire the events.
public void put()
{
for (int i = 0; i < 3; i++)
if (evnt[i] != null) evnt[i]();
}

}

 

class abc
{
public void putabc()
{
Console.WriteLine("Event received by abc object");
}
}

class xyz
{
public void putxyz()
{
Console.WriteLine("Event received by xyz object");
}
}

class pqr
{
public void putpqr()
{
Console.WriteLine("Event received by pqr object");
}
}

class mno
{
public void putmno()
{
Console.WriteLine("Event received by mno object");
}
}

class sample
{
public static void Main()
{
MyEvent et = new MyEvent();
abc x = new abc ();
xyz y = new xyz ();
pqr z = new pqr();
mno k = new mno();

        Console.WriteLine("Adding events.");

        et.evt += x.putabc ;
et.evt += y.putxyz ;
et.evt += z.putpqr ;

        // Can't store this one -- full.
et.evt += k.putmno ;

        Console.WriteLine();
et.put();

Console.WriteLine("Remove xyz put.");
et.evt -= x.putabc;
et.put();

        Console.WriteLine("Add k object");
et.evt  += k.putmno;
et.put();

    }
}

EventArgs
C# allows you to write any type of event that you desire. At the core of these guidelines is the requirement that event handlers have two parameters. The first is a reference to the object that generated the event. The second is a parameter of type EventArgs that contains any other information required by the handler.

Program on EventArgs

using System;

class MyEventArgs : EventArgs
{
public int num;
}

 

delegate void visionevent(object source, MyEventArgs arg);

 

class abc
{
static int count = 0;

    public event visionevent  evnt;

 
public void put()
{
MyEventArgs arg = new MyEventArgs();

        if (evnt  != null)
{
arg.num  = count++;
evnt (this, arg);
}
}
}

class xyz
{
public void xyzput(object source, MyEventArgs arg)
{
Console.WriteLine("Event " + arg.num  +
" received by an Xyz object.");
Console.WriteLine("Source is " + source);
Console.WriteLine();
}
}

class pqr
{
public void pqrput(object source, MyEventArgs arg)
{
Console.WriteLine("Event " + arg.num +
" received by a pqr object.");
Console.WriteLine("Source is " + source);
Console.WriteLine();
}
}

class sample
{
public static void Main()
{
pqr  ob1 = new pqr ();
xyz ob2 = new xyz();
abc  evt = new abc();

        evt.evnt  += ob1.pqrput;
evt.evnt  += ob2.xyzput;

        evt.put();
evt.put();


}
}